package com.soonfor.evaluate.tools.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import com.estronger.shopmanage.R;


public class SelectPicPopupWindow extends PopupWindow {
    private Button btn_camera;
    private Button btn_image;
    private Button btfquxiao;
    private View mMenuView;

    /**
     *
     * @param context
     * @param itemdesc item的中文描述
     * @param itemsOnClick 点击事件
     */
    public SelectPicPopupWindow(final Context context, String[] itemdesc, View.OnClickListener itemsOnClick) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.boot_layout_popup, null);
        btn_camera = (Button) mMenuView.findViewById(R.id.btfScanExternal);
        btn_image = (Button) mMenuView.findViewById(R.id.btfLocalImage);
        btfquxiao = (Button) mMenuView.findViewById(R.id.btfquxiao);
        if(itemdesc == null){
            btn_camera.setText("相机扫描外部二维码");
            btn_image.setText("本地二维码图片识别");
        }else {
            btn_camera.setText(itemdesc[0]);
            btn_image.setText(itemdesc[1]);
        }
        //设置按钮监听
        btn_camera.setOnClickListener(itemsOnClick);
        btn_image.setOnClickListener(itemsOnClick);
        btfquxiao.setOnClickListener(itemsOnClick);
        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(false);
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        //设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.dialogStyle);
        backgroundAlpha((Activity)context, 0.5f);//0.0-1.0
        //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        mMenuView.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {

                int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                int y = (int) event.getY();
                if (event.getAction() == MotionEvent.ACTION_UP) {
                   backgroundAlpha((Activity)context, 1f);
                   dismiss();
                }
                return true;
            }
        });
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public static void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

}
